/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.hutech09.jsale.web.manager.model;

import java.util.List;
import java.util.Map;
import org.hutech09.jsale.bsn.ICustomerManager;
import org.hutech09.jsale.entity.Customer;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

/**
 *
 * @author Nam
 */
public class LazyCustomerDataModel extends LazyDataModel<Customer> {

    private ICustomerManager customerManager;

    public LazyCustomerDataModel(ICustomerManager customerManager) {
        this.customerManager = customerManager;
    }

    @Override
    public Object getRowKey(Customer object) {
        return object.getId();
    }

    @Override
    public Customer getRowData(String rowKey) {
        return customerManager.find(rowKey);
    }

    @Override
    public List<Customer> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {
        List<Customer> result;
        if (sortOrder == SortOrder.DESCENDING) {
            result = customerManager.findPaging(first, pageSize, sortField, true, filters);
        } else {
            result = customerManager.findPaging(first, pageSize, sortField, false, filters);
        } 
        setRowCount((int) customerManager.count(filters));
        return result;
    }
}
